package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao implements BaseStudentDao{
    private static Student[] students = new Student[5];

    static {
        Student stu1 = new Student("heima001","张三","23","1991-1-1");
        Student stu2 = new Student("heima002","李四","24","1990-1-1");

        students[0] = stu1;
        students[1] = stu2;
    }

    public Student[] findAllStudent() {
            return students;
    }

    public boolean addStudent(Student student) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student1 = students[i];
            if (student1 == null) {
                index = i;
            }
        }
        if (index == -1) {
            return false;
        } else {
            students[index] = student;
            return true;
        }
    }

    public void deleteStudentInfo(String deleteId) {
        //拿到删除id对应的索引
        int index = getIndex(deleteId);
        //拿到id索引，将其内容赋值为null
        students[index] = null;
    }

    public void updataStudentInfo(Student student,String updataId) {
        //拿到需要修改的id索引
        int index = getIndex(updataId);
        //将索引处的值修改为student
        students[index] = student;
    }

    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
}
